package com.obst.data.repository;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import com.obst.data.pojo.UserInfo;

public class UserRepository{

	public void addUser(UserInfo user) {

		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			pm.makePersistent(user);
		} finally {
			pm.close();
		}
	}
	
	private UserInfo getUserInfoByFilter(String filterName, String  Value) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query q = pm.newQuery(UserInfo.class);
		q.setFilter(filterName + " == '" + Value + "'");
		
		try {
			@SuppressWarnings("unchecked")
			List<UserInfo> userInfo= (List<UserInfo>) q.execute();
			if(userInfo.isEmpty() != true) {
				return userInfo.get(0);
			} else {
				return new UserInfo(null, null, null, null, null, 0, 0, null);
			}
		} finally {
			q.closeAll();
		}
	}
	
	public UserInfo  getUserByEmail(String email) {
		return this.getUserInfoByFilter("email", email);
	}
	
	/*
	public User getUser(long AccountID) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		String query = "SELECT from " + User.class.getName();
		
		Query q = pm.newQuery(User.class);
		q.setFilter("AccountID == " + AccountID);
		try {
			List<User> users = (List<User>) q.execute();
			return users.get(0);
		} finally {
			User resUser = new User((long) 0, "noname","");
			q.closeAll();
			return resUser;
		}
	}
	*/
}
